Emacs as Godot Editor

Emacs everywhere

As a long-time Emacs user I always struggled with the Godot editor. Not that it is particularly bad but my Emacs muscle memory kicks in whenever I’m editing text. That can be quite an annoyance but it is also a mental hurdle. When editing code I want to spent all my scarce brain ressources on that process and don’t want to be burdened with anything else. But help is on the way in the form of emacs-gdscript-mode by Nathan Lovato, founder of GDQuest.

Install and Setup

The package is available via MELPA and on Github. The Github page is an excellent ressource which covers everything you need to know. So here are just the basics to get you started.

As the package is available via MELP the install is pretty easy: M-x package-install gdscript-mode. After that you can simply require the package in your init.el: (require 'gdscript-mode).

Or, if you are using Doom Emacs as I do, just enable it in yout init.el with (gdscript +lsp). This will not only install the gdscript-mode but also enable code completion via LSP (which is burden to configure in vanilla Emacs or even Spacemacs). Don’t forget to resync your Doom configuration in the shell via ~/.emacs.d/bin/doom sync and reload it in Emacs by M-x doom/reload.

You will also want to install the Python package gdtoolkit to make use of the auto formater. Just use pip on the console: pip3 install gdtoolkit.

Before jumping right into the action you sould also configure two variables to make complete use of the gdscript-mode. Those are gdscript-godot-executable and gdscript-docs-local-path. The first one tells Emacs where to find the Godot executable, which is needed to run your code from within Emacs. The second lets you browse the documentation right within Emacs via eww (or whatever browser you’ve setup). As a Doom Emacs user I’ve added those in config.el:

(after! gdscript-mode
  :config
  (setq gdscript-godot-executable "/home/user/bin/godot/godot")
  (setq gdscript-docs-local-path "/home/user/Documents/godot/godot-docs-html-stable"))

Now you can use M-x gdscript-open-godot-project-in-editor and M-x gdscript-godot-run-project-debug right from within Emacs or M-x gdscript-docs-browse-api and M-x gdscript-docs-browse-symbol-at-point.

Note, that to browse the documentaion from Emacs locally you need of course download the latest documentation first and unzip to a directcory of your choice.

Usage

And voilà, there you have it! Emacs as a fully featured Godot editor! Whenever you’ve opened a GDScript file, your new best friend now is SPC m which will display a nifty hydra which all of the cool features gdscript-mode comes with:

SPC m f will give you access to the formatting features of gdscript-mode. You have the option to format the buffer or the region. Unfortunately there is no hydra for gdscript-format-all which will format all gdscript files in your current project. But this is easily added in your config.el:

    (after! gdscript-mode
        :config
        ;; …
        (map! :localleader
            :map gdscript-mode-map
            (:prefix ("f" . "format")
            :desc "Format project"
            "p" #'gdscript-format-all)))

SPC m r will let you run your project, scene or open it in the Godot editor. It even lets you run your project in debug mode which brings us to the next hydra.

SPC m d gives you acces to some basic debugging features like breakpoints and stepping. gdscript-mode comes with quite a bit more options for debugging which you can explore by invoking M-x gdscript-debug- and see what comes up.

SPC m h finally is for getting help. You can either browse the API online (if you haven’t installed it locally as mentioned above) or call help for symbol-at-point.

Another key combo worth mentioning is SPC s i which opens imenu (if installed). This is not part of gdscript-mode but a very handy way to jump around in your code. Speaking of jumping around: I would also suggest to get familiar with projectile. Not only does it make getting around in the files more easy, but with projectile you can find and rename variables, functions, classes and whatnot in a breeze. Refactoring made easy!

Other Options

For the sake of completeness it should also be mentioned that there is godot-gdscript.el. By the looks of it it comes with some nice features, like Org-Mode support. But I haven’t tried it out yet as GDQuest’s mode seems to be more active and feature complete. But please do not rely on my judgement and have a look for yourself!